<title>数据查询(所有)</title>
<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb" lay-separator=">">
        <a lay-href=""><i class="layui-icon layui-icon-locate"></i>您当前的位置：</a>
        <a><cite>指标数据</cite></a>
        <a><cite>数据查询(所有)</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="manage-top">
        <div class="layui-card">
            <div class="layui-card-header">
                <div class="layui-header-title">
                    <i class="layui-icon audit-icon"></i>数据列表
                </div>
            </div>
            <div class="layui-card-body">
                <table id="auditManage-table" lay-filter="auditManage-table-filter"></table>
            </div>
        </div>
    </div>
    <div class="manage-bot">
        <div class="layui-row layui-col-space12">
            <div class="layui-col-md3 depart">
                <table id="custom-depart-table" lay-filter="depart-table-filter"></table>
            </div>
            <div class="layui-col-md9">
                <div class="layui-card">
                    <div class="layui-form" lay-filter="custom-indexdata-table">
                        <div class="layui-card-body">
                            <div class="layui-btn-container">
                                <button type="button" class="layui-btn layui-btn-score T-btn" data-target="score">查看得分</button>
                            </div>
                            <table id="my-custom-table" lay-filter="indicators-table"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .manage-top .layui-table{
        width: 100%;
    }
    .manage-top .layui-table thead tr td{
        padding: 8px 15px;
        border: none;
    }
    .layui-header-title{
        padding-left: 30px;
        position: relative;
    }
    .layui-card-header{
        border-bottom: 1px solid #D0D3DB;
    }
    .layui-card-header .audit-icon{
        width: 19px;
        height: 19px;
        background: url('/dist/style/res/param/auditManage/audit.png') no-repeat center center;
        background-size: cover;
        margin-top: -10px;
        left: 0px;
    }
    .layui-table-view .layui-table td, .layui-table-view .layui-table th{
        padding: 4px 0;
    }
    .manage-top .layui-table-cell{
		height: inherit;
	}
    .my-btn-audit, .my-btn-history, .my-btn-allDown, .layui-btn-downFile{
        width: 82px;
        height: 25px;
        line-height: 25px;
        padding: 0;
        background: transparent;
    }
    .my-btn-audit{
        border: 1px solid #4BAA0B;
        color: #4BAA0B;
    }
    .my-btn-allDown, .layui-btn-downFile{
        border: 1px solid #5B80D6;
        color: #5B80D6;
    }
    .my-btn-history{
        border: 1px solid #5B80D6;
        color: #5B80D6;
    }
    .my-btn-audit:hover, .my-btn-allDown:hover, .layui-btn-downFile:hover{
        color: #F9C971;
        border-color: #F9C971;
    }
    .manage-top .layui-badge {
		height: 23px;
		line-height: 23px;
		border-radius: 10px;
        padding: 0 21px;
	}
    .layui-table tbody tr.lineactive {
        background-color: #FDE7A3 !important;
    }
    .layui-table[lay-even] tbody tr:nth-child(odd) {
        background-color: #D3E5FF;
    }
    .layui-table[lay-even] tbody tr:nth-child(even) {
        background-color: #fff;
    }
    .layui-table[lay-even] tbody tr:nth-child(odd):hover, .layui-table[lay-even] tbody tr:nth-child(even):hover{
        background-color: #f2f2f2; 
    }
    .manage-bot{
        margin-top: 13px;
    }
    .manage-bot .layui-card{
        height: 610px;
    }
    .manage-bot tr td{
        text-align: center;
        vertical-align: middle;
    }
    .layui-btn-save, .layui-btn-pass, .layui-btn-return, .layui-btn-download{
        width: 68px;
        height: 30px;
        line-height: 30px;
        color: #FFFFFF;
        border-radius: 4px;
        padding: 0;
        background-color: #2669B1;
    }
    .layui-btn-download{
        width: 100px;
    }
    .layui-btn-save{
        padding-left: 20px;
        background: url('/start/TheForm/css/img/form-save.png') no-repeat 9px 7px #2669B1;
    }
    .layui-btn-score{
        width: 102px;
        height: 30px;
        line-height: 30px;
        color: #FFFFFF;
        padding-left: 32px;
        background: url('/dist/style/res/param/auditManage/defen.png') no-repeat 10px 8px #2669B1;
    }
    .my-btn-audit:hover, .my-btn-history:hover{
        color: orange;
        border-color: orange;
    }
    .my-custom-icon-folder{
        width: 22px;
        height: 16px;
        line-height: 16px;
        position: relative;
    }
    .my-custom-icon-folder::after{
        background-image: url('/dist/style/res/param/auditManage/icon-folder.png');
    }
    .ew-tree-table .my-custom-icon-folder:after {
        content: "";
        width: 20px;
        height: 18px;
        position: absolute;
        left: 0;
        top: -3px;
        background-size: cover;
        background-image: url('/dist/style/res/param/auditManage/icon-folder.png');
    }
    .layui-btn-record{
        height: 28px;
        line-height: 28px;
        background-color: #2669B1;
        margin-left: 10px;
    }
    .depart .layui-table-view {
        margin: 0;
        background-color: #FFF;
    }
    .layui-btn-downFile, .my-btn-audit{
        width: 65px;
    }
</style>

<!-- 状态模板 -->
<script type="text/html" id="temporarystatusTpl">
    {{#
		var getStatusColor = function() {
			var mapColors = {
                '00': 'layui-bk-green',
                '05': 'layui-bk-orange',
                '20': 'layui-bk-gray',
                '25': 'layui-bk-orange',
                '30': 'layui-bk-green',
                '40': 'layui-bk-orange',
                '90': 'layui-bk-gray',
                '99': 'layui-bk-gray'
			};
			return mapColors[d.state] || '';
		};
	}}
	<span class="custom-badge {{getStatusColor()}}">{{d.statename}}</span>	
</script>

<!-- 附件模板 -->
<script type="text/html" id="temporaryenclosureTpl">
    {{#  if(!!d.fileMsg && d.fileMsg.length && d.fileMsg.length > 0){ 
        $.each(d.fileMsg, function(index, file) {
    }}
        <a class="T-btn" data-url="{{file.url}}" data-target="downFile" href="javascript:void(0);" lay-tips='{{file.filename}}'>{{file.filename}}</a>
    {{#  }) } else { }}
        
    {{#  } }}
</script>

<!-- 单位任务附件模板 -->
<script type="text/html" id="departTpl">
    {{#  if(!!d.fileList && d.fileList.length && d.fileList.length > 0){ 
        layui.each(d.fileList, function(index, file) {
    }}
        <a class="T-btn" lay-event="downFile" href="javascript:void(0);" lay-tips='{{file.filename}}'>{{file.filename}}</a>
    {{#  }) } else { }}
        
    {{#  } }}
</script>

<!-- 操作栏模板 -->
<script type="text/html" id="temporary-table-operate-bar">
    <button class="layui-btn my-btn-allDown" lay-event="allDown">批量下载</button>
</script>

<!-- 输入框模板 -->
<script type="text/html" id="datascore">
    {{# if(d.examine === 0){ }}
        <button type="button" class="layui-btn layui-btn-sm layui-btn-warn T-btn" data-target="examine" data-examine="{{d.examine_record}}"></button>
    {{# } }}
    {{# if(!!d.write_value){ }}
        <input type="text" name="percent{{d.LAY_TABLE_INDEX}}" id="percent{{d.LAY_TABLE_INDEX}}" data-id="{{d.id}}" data-itemId="{{d.item_id}}" data-showVal="{{d.show_value}}" data-writeVal="{{d.write_value}}" class="layui-input my-input-style" value="{{d.write_value}}"/>
    {{# }else if(!!d.show_value){ }}
        <input type="text" name="percent{{d.LAY_TABLE_INDEX}}" id="percent{{d.LAY_TABLE_INDEX}}" data-id="{{d.id}}" data-itemId="{{d.item_id}}" data-showVal="{{d.show_value}}" data-writeVal="{{d.write_value}}" class="layui-input my-input-style" value="{{d.show_value}}"/>
    {{# }else if(!!d.datascore){ }}
        <input type="text" name="percent{{d.LAY_TABLE_INDEX}}" id="percent{{d.LAY_TABLE_INDEX}}" data-id="{{d.id}}" data-itemId="{{d.item_id}}" data-showVal="{{d.show_value}}" data-writeVal="{{d.write_value}}" class="layui-input my-input-style" value="{{d.datascore}}"/>
    {{# }else{ }}
        <input type="text" name="percent{{d.LAY_TABLE_INDEX}}" id="percent{{d.LAY_TABLE_INDEX}}" data-id="{{d.id}}" data-itemId="{{d.item_id}}" data-showVal="{{d.show_value}}" data-writeVal="{{d.write_value}}" class="layui-input my-input-style" value=""/>
    {{# } }}
    <span>请输入数值。</span>
</script>

<script>
    layui.use(['admin', 'table', 'tools', 'form', 'view', 'tableMerge'], function(){
        var admin = layui.admin,
            table = layui.table,
            tools = layui.tools,
            form = layui.form,
            view = layui.view,
            tableMerge = layui.tableMerge;
        
        var taskId = '',
            deptId = '',
            zbType = '',
            layout = ['.layui-table th', '.layui-table td'];

        form.render(null, 'custom-indexdata-table');
        initauditTable(); //加载审核任务表格

        function initauditTable(){
            table.render({
                elem: '#auditManage-table',
                url: 'myapi/DC/api/rest/web/custom/DCGetSJCXAllTaskList',
                where: {
                    access_token: layui.data('layuiAdmin').access_token
                },
                height: 150,
                even: true,
                skin: 'nob',
                page: {
                    groups: 1, 
                    limit: 10,
                    next:'<i class="layui-icon">&#xe602;</i>',
                    prev:'<i class="layui-icon">&#xe603;</i>',
                    first: '<i class="layui-icon">&#xe65a;</i>',
                    last: '<i class="layui-icon">&#xe65b;</i>',
                    layout: ['refresh', 'first', 'prev', 'skip', 'allPage', 'next', 'last'],
                    extend:{
                        first: true, //是否显示首页
                        spr: false,//是否显示页码分页符号 默认显示
                        pagecount: true//页码是否显示总数  groups=1时才有效
                    }
                },
                text: {
                    none: '<div class="no-data-tips"><img src="/dist/style/res/about/empty.png"><p style="color:#DBDBDB;">暂无数据</p></div>'
                },
                request: {
                    limitName: 'count'
                },
                parseData: function(res){
                    var data = res.data;
                    return {
                        "code": data.retcode, //解析接口状态
                        "msg": data.retmsg, //解析提示文本
                        "count": data.data.totalNumber, //解析数据长度
                        "data": data.data.data //解析数据列表
                    };
                },
                cols: [[
                    {
                        field: 'taskname',
                        title: '任务名称',
                        align: 'center'
                    }, {
                        field: 'state',
                        title: '状态',
                        align: 'center',
                        templet: '#temporarystatusTpl'
                    }, {
                        field: 'dept_name',
                        title: '填报单位',
                        align: 'center'
                    }, {
                        field: 'starttime',
                        title: '开始时间',
                        align: 'center',
                        templet: function(d) {
                            return d.starttime.substring(0, 10);
                        }
                    }, {
                        field: 'endtime',
                        title: '结束时间',
                        align: 'center',
                        templet: function(d) {
                            return d.endtime.substring(0, 10);
                        }
                    }, {
                        field: 'fileMsg',
                        title: '附件名称',
                        align: 'center',
                        templet: '#temporaryenclosureTpl'
                    },{
                        field: 'memo',
                        title: '说明',
                        align: 'center'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        toolbar: '#temporary-table-operate-bar'
                    }
                ]],
                done: function(res, curr, count){
                    if(res.data && res.count > 0){
                        var data = res.data;
                        taskId = data[0].id;
                        zbType = data[0].zbtype;
                        $('[lay-id="auditManage-table"]').find('tbody tr').eq(0).addClass('lineactive');
                        initDepartTable(taskId, zbType);
                    }
                }
            })
        }

        /* 加载单位任务表格
        *@param taskid 任务id
        */
        function initDepartTable(taskid, zbtype){
            var zbtype = zbtype || '';
            admin.req({
                url: 'myapi/DC/api/rest/web/custom/DCGetTaskDepeTable',
                data: {
                    taskid: taskid
                },
                done: function(res){
                    var result = res.data;
                    if(result.retcode == 0){
                        table.render({
                            elem: '#custom-depart-table',
                            height: 610,
                            text: {
                                none: '<div class="no-data-tips"><img src="/dist/style/res/about/empty.png"><p style="color:#DBDBDB;">暂无数据</p></div>'
                            },
                            limit: 10000,
                            data: result.data,
                            cols: [[
                                { field: 'dept_name', title: '单位名称', align: 'center',
                                    templet: function(d){
                                        return '<span lay-tips='+d.dept_name+'>'+d.dept_name+'</span>'
                                    }
                                },
                                { field: 'filename', title: '附件名称', templet: '#departTpl', align: 'center' }
                                // { field: 'url', title: '操作', toolbar: '#oplTpl', align: 'center'}
                            ]],
                            done: function(res, curr, count){
                                if(res.data && res.count > 0){
                                    deptId = res.data[0].dept_id;
                                    if(zbtype == '2'){  //省级任务
                                        $('[lay-id="custom-depart-table"]').find('tbody tr').eq(0).addClass('lineactive');
                                        initTable(taskId, deptId);
                                    }else{ //国家级 默认不用传deptid
                                        $('[lay-id="custom-depart-table"]').find('tbody tr').eq(0).removeClass('lineactive');
                                        initTable(taskId);
                                    }
                                }
                            }
                        })
                    }
                }
            })
        }

        /* 加载指标数据表格
        *@param taskid 任务id
        */
        function initTable(taskid, deptid){
            var param = { taskid: taskid },
            deptid = deptid || '';
            if(deptid){ param.dept_id = deptid }
            admin.req({
                url: 'myapi/DC/api/rest/web/custom/DCGetSJCXAllDatascore',
                data: param,
                done: function(res){
                    if(res.data.retcode == 0){
                        var head = res.data.data.head,
                            data = res.data.data.data,
                            toolCols = [
                                {field: 'datascore', templet: '#datascore', align: 'center', width: 200},
                                {field: 'history', align: 'center', width: 200}
                            ];
                        for(var i = 0; i < head.length; i++){
                            if(i == 0){ head[i].merge = true; }
                            else{ head[i].merge = [head[i-1].field, head[i].field]; }
                            head[i].minWidth = 200;
                            head[i].align = 'center';
                            (function(i){ 
                                head[i].templet = function(d){
                                    var field = d[head[i].field];
                                    if(field != 'datascore'){
                                        return '<span lay-tips='+field+'>'+field+'</span>' ;
                                    }
                                };
                            })(i);
                        }
                        head = head.concat(toolCols);
                        table.render({
                            elem: '#my-custom-table',
                            height: 560,
                            text: {
                                none: '<div class="no-data-tips"><img src="/dist/style/res/about/empty.png"><p style="color:#DBDBDB;">暂无数据</p></div>'
                            },
                            limit: 10000,
                            data: data,
                            cols: [head],
                            done: function(res, curr, count){
                                if(res.data && res.data.length > 0){
                                    tableMerge.render(this);
                                    var allTrs = $('[lay-id="my-custom-table"] tbody tr');
                                    $.each(allTrs, function(index, item){
                                        var btnNode = '<button type="button" class="layui-btn layui-btn-record T-btn" data-target="record" data-id='+res.data[index].id+'>查看记录</button>';
                                        $(this).find('td:last-of-type .layui-table-cell').append(btnNode);
                                    })
                                }
                                tools.setFont(layout);
                            }
                        })
                    }
                }
            })
        }

        //监听任务表格行单双击事件
		table.on('row(auditManage-table-filter)', function(obj){
            var data = obj.data;
			$('[lay-id="auditManage-table"]').find(obj.tr.selector).addClass('lineactive');
            $('[lay-id="auditManage-table"]').find(obj.tr.selector).siblings().removeClass('lineactive');
            taskId = data.id;
            zbType = data.zbtype;
            initDepartTable(taskId, zbType);
        })

        //监听单位任务表格行点击事件
        table.on('row(depart-table-filter)', function(obj){
            var data = obj.data;
			$('[lay-id="custom-depart-table"]').find(obj.tr.selector).addClass('lineactive');
			$('[lay-id="custom-depart-table"]').find(obj.tr.selector).siblings().removeClass('lineactive');
            deptId = data.dept_id;
            initTable(taskId, deptId);
        })

        //监听任务表格工具栏
        table.on('tool(auditManage-table-filter)', function(obj){
            var event = obj.event,
                data = obj.data;
            switch(event){
                case 'allDown':
                    if(data.id){
                        location.href = 'myapi/DC/api/rest/web/custom/IndexTaskFilePackDownload?taskid='+(data.id)+'&access_token='+layui.data('layuiAdmin').access_token
                    }else {
                        layer.msg('暂无下载附件的任务', {time: 1500});
                    }
                break;
                default: 
                break;
            }
        })

        //监听单位任务表格工具栏
        table.on('tool(depart-table-filter)', function(obj){
            var event = obj.event,
                data = obj.data;
            switch(event){
                case 'downFile':
                    admin.req({
                        url: 'myapi/DC/api/rest/web/custom/IndexTaskFilePackDownload',
                        data: {
                            taskid: taskId,
                            deptid: data.dept_id
                        },
                        done: function(res){
                            if(res.retcode == 0){
                                layer.msg(res.retmsg, {time: 1500});
                            }
                        }
                    })
                break;
                default: 
                break;
            }
        })
        

        //监听submit提交
        form.on('submit(fill-data-submit)', function(data){
            delete data.field['app'];
            var field = data.field,
                val = [],
                fillData = [];
            for(var i in field){
                if($.trim(field[i])){
                    var reg  = new RegExp(/^\d+(?=\.{0,1}\d+$|$)/);
                    if(!reg.test(field[i])){
                        $('#'+i).focus();
                        layer.msg('请输入数字');
                        return ;
                    }
                    val.push(i);
                }
            }
            $.each(val, function(index, item){
                var id = $('#'+item).attr('data-id'),
                    datascore = $('#'+item).val();
                fillData.push({ id: id, datascore: datascore });
            })
            if(fillData && fillData.length > 0){
                admin.req({
                    url: 'myapi/DC/api/rest/web/custom/IndexTaskFillDataSave',
                    type: 'post',
                    data: {
                        data: JSON.stringify(fillData)
                    },
                    done: function(res){
                        if(res.data.retcode == 0){
                            initTable(taskId, deptId);
                            layer.msg(res.data.retmsg, {time: 1500});
                        }
                    }
                })
            }else {
                layer.msg('请填写数据之后再保存', {time: 1500});
            }
        })
        
        //点击事件
        $('.manage-bot').delegate('.T-btn', 'click', function(){
            var target = $(this).data('target');
            if(target == 'pass' || target == 'return'){
                    var checkStatus = table.checkStatus('my-custom-table'),
                        checkData = checkStatus.data,
                        memoTitle = '';
                    if(checkData && checkData.length > 0){
                        if(target == 'pass'){
                            memoTitle = '补充说明';
                        }else {
                            memoTitle = '退回原因';
                        }
                        layer.open({
                            id: 'usermemo',
                            type: 1,
                            title: '<i class="formFill-icon"></i><span style="color:#FFF;">'+memoTitle+'</span>',
                            area: ['300px', '290px'],
                            offset: 'auto',
                            shadeClose: true,
                            resize: false,
                            success: function(lay, index){
                                view(this.id).render('./audit/components/usermemo', {
                                    showReview: false,
                                    title: '审批意见'
                                });
                            },
                            btn: ['确定', '取消'],
                            btnAlign: 'c',
                            yes: function(index, layero){
                                var memoval = $('#usermemo .layui-textarea').val(),
                                    auditVal = [],
                                    model = target == 'pass' ? 'pass' : 'back';
                                layui.each(checkData, function(index, item){
                                    auditVal.push({ id: item.id, usermemo: memoval });
                                })
                                admin.req({
                                    url: 'myapi/DC/api/rest/web/custom/IndexTaskDataPassAndBack',
                                    type: 'post',
                                    data: {
                                        model: model,
                                        data: JSON.stringify(auditVal)
                                    },
                                    done: function(res){
                                        if(res.data.retcode == 0){
                                            initTable(taskId, deptId);
                                            layer.msg(res.data.retmsg, {time: 1500});
                                        }
                                    }
                                });
                                layer.close(index);
                            },
                            btn2: function(index, layero){
                                layer.close(index);
                            }
                        })
                    }else {
                        layer.msg('请勾选需要提交审核的数据', {time: 1500});
                    }
            }else if(target == 'downFile'){
                var url = 'myapi/DC'+ $(this).data('url'),
                    name = $(this).text();
                tools.downMethodfunction(url, name);
            }else if(target == 'record'){
                var dataId = $(this).data('id');
                admin.req({
                    url: 'myapi/DC/api/rest/web/custom/GetIndexTaskOperatingData',
                    data: {
                        taskid: dataId
                    },
                    done:function(res){
                        var result = res.data;
                        if(result.retcode == 0){
                            if(result.data && result.data.length > 0){
                                layer.open({
                                    id: 'temporary-addhtml',
                                    title:'<i class="formFill-icon"></i><span style="color:#FFF;">查看记录</span>',
                                    offset: '110px',
                                    type: 1,
                                    shadeClose: true,
                                    area: ['350px', '400px'],
                                    resize: false,
                                    success: function(lay, index){
                                        view(this.id).render('./task/temporary/history', {history: result.data});
                                    }
                                })
                            }else{
                                layer.msg('暂无记录', {time: 1500});
                            }
                        }
                    }
                })
            }else if(target == 'allDown'){
                if(taskId){
                    location.href = 'myapi/DC/api/rest/web/custom/IndexTaskFilePackDownload?taskid='+taskId+'&access_token='+layui.data('layuiAdmin').access_token
                }else {
                    layer.msg('暂无下载附件的任务', {time: 1500});
                }
            }else if(target == 'eachDown'){
                if(taskId){
                    layer.open({
                        id: 'file-table',
                        title:'<i class="formFill-icon"></i><span style="color:#FFF;">分省下载</span>',
                        offset: '110px',
                        type: 1,
                        shadeClose: true,
                        area: ['500px', '450px'],
                        resize: false,
                        success: function(lay, index){
                            view(this.id).render('./param/component/fileList', {taskid: taskId});
                        }
                    })
                }else {
                    layer.msg('暂无下载附件的任务', {time: 1500});
                }
            }else if(target == 'score'){
                var departTasks = $('[lay-id="custom-depart-table"] tbody tr'),
                    flag = false,
                    param = { taskid: taskId, isAll: 'false' };
                layui.each(departTasks, function(index, item){
                    if($(this).hasClass('lineactive')){
                        flag = true;
                    }
                });
                if(flag){ param.deptid = deptId }
                if(zbType == '2' && !flag){
                    layer.msg('请选择单位任务', {time: 1500});
                    return ;
                }
                layer.open({
                    id: 'viewScoew-html',
                    title:'<i class="formFill-icon"></i><span style="color:#FFF;">查看得分</span>',
                    type: 1,
                    offset: '20px',
                    shadeClose: true,
                    area: ['1400px', '650px'],
                    resize: false,
                    success: function(lay, index){
                        view(this.id).render('./param/component/viewScore', param);
                    }
                })
            }else if(target == 'examine'){
                var recordHtml = $(this).data('examine').replace(/</g, '&lt;').replace(/>/g, '&gt;');
                layer.open({
                    id: 'examinePage',
                    title:'<i class="formFill-icon"></i><span style="color:#FFF;">格检详情</span>',
                    offset: '110px',
                    type: 1,
                    shadeClose: true,
                    area: ['400px', '300px'],
                    resize: false,
                    content: '<div class="layui-card-body">'+recordHtml+'</div>'
                })
            }
        })
    })
</script>